package com.gitee.linmt.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gitee.linmt.base.BaseEntity;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Collection;

/**
 * 用户信息.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@Entity
@Table(name = "sys_user")
public class SysUser extends BaseEntity implements UserDetails, CredentialsContainer {
    
    private String username;
    
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String secretCode;
    
    private String gender;
    
    private String phoneNumber;
    
    private String emailAddress;
    
    private int accountNonExpired;
    
    private int accountNonLocked;
    
    private int credentialsNonExpired;
    
    private int enabled;
    
    @Transient
    private Collection<? extends GrantedAuthority> authorities;
    
    @Basic
    @Override
    @Column(name = "username")
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    @Basic
    @Column(name = "secret_code")
    public String getSecretCode() {
        return secretCode;
    }
    
    public void setSecretCode(String secretCode) {
        this.secretCode = secretCode;
    }
    
    @Basic
    @Column(name = "gender")
    public String getGender() {
        return gender;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
    
    @Basic
    @Column(name = "phone_number")
    public String getPhoneNumber() {
        return phoneNumber;
    }
    
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    
    @Basic
    @Column(name = "email_address")
    public String getEmailAddress() {
        return emailAddress;
    }
    
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    
    @Basic
    @Column(name = "account_non_expired")
    public int getAccountNonExpired() {
        return accountNonExpired;
    }
    
    @Basic
    @Column(name = "account_non_locked")
    public int getAccountNonLocked() {
        return accountNonLocked;
    }
    
    @Basic
    @Column(name = "credentials_non_expired")
    public int getCredentialsNonExpired() {
        return credentialsNonExpired;
    }
    
    @Basic
    @Column(name = "enabled")
    public int getEnabled() {
        return enabled;
    }
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
    
    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
    
    @Override
    @Transient
    @JsonIgnore
    public String getPassword() {
        return getSecretCode();
    }
    
    @Override
    @Transient
    public boolean isAccountNonExpired() {
        return 0 == this.accountNonExpired;
    }
    
    public void setAccountNonExpired(int accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }
    
    @Override
    @Transient
    public boolean isAccountNonLocked() {
        return 0 == this.accountNonLocked;
    }
    
    public void setAccountNonLocked(int accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }
    
    @Override
    @Transient
    public boolean isCredentialsNonExpired() {
        return 0 == this.credentialsNonExpired;
    }
    
    public void setCredentialsNonExpired(int credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }
    
    @Override
    @Transient
    public boolean isEnabled() {
        return 1 == this.enabled;
    }
    
    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }
    
    @Override
    public void eraseCredentials() {
        this.secretCode = null;
    }
    
    @Override
    public String toString() {
        return "SysUser{" + "username='" + username + '\'' + ", secretCode='" + secretCode + '\'' + ", gender='"
                + gender + '\'' + ", phoneNumber='" + phoneNumber + '\'' + ", emailAddress='" + emailAddress + '\''
                + ", accountNonExpired=" + accountNonExpired + ", accountNonLocked=" + accountNonLocked
                + ", credentialsNonExpired=" + credentialsNonExpired + ", enabled=" + enabled + ", authorities="
                + authorities + '}';
    }
}
